package net.sf.rose.jdbc;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fengjian
 * @since 2005/04/26
 */
public class PageBean {

	/** 页面跳转常量 */
	public static final String JUMP_PAGE_STEP_FIRST = "FIRS";     // 首页
	public static final String JUMP_PAGE_STEP_PREV = "PREV";   // 上一页
	public static final String JUMP_PAGE_STEP_CURR = "CURR";  // 当前页
	public static final String JUMP_PAGE_STEP_NEXT = "NEXT";  // 下一页
	public static final String JUMP_PAGE_STEP_LAST = "LAST";   // 未页

	/** 页面名称常量 */
	public static String PAGING_PAGE_SIZE = "pageSize";               // 总页数名称
	public static String PAGING_CURRENT_PAGE = "currentPage"; // 当前页名称

	/** 每页的记录数 */
	private int pageSize = 15;

	/** * 当前页 */
	private int currentPage = 1;

	/** 记录总数 */
	private int recordCount = -1;

	/** * 总页数 */
	private int pageCount = -1;

	/** 用于分页的记录元数据 */
	private List<Object> records = null;

	/**
	 * 设置每页的记录数
	 */
	public void setpageSize(int pageSize) {
		this.pageSize = pageSize < 1 ? this.pageSize : pageSize;
	}

	/**
	 * 取得每页的记录数
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 取得当前页码
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * 设置当前页
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage < 1 ? 1 : currentPage;
	}

	/**
	 * 加载分页的记录元数据
	 */
	public void setRecords(List<Object> records) {
		this.records = records;
	}

	/**
	 * 取得分页的记录元数据
	 */
	public List<Object> getRecords() {
		if(records == null ){
			records = new ArrayList<Object>();
		}
		return records;
	}

	/**
	 * 取得记录总数
	 */
	public long getRecordCount() {
		return this.recordCount;
	}

	/**
	 * 设置记录总数
	 */
	public void setRecordCount(int recordCount) {
		this.recordCount = recordCount;
		pageCount = recordCount / pageSize;
		if (recordCount % pageSize != 0) {
			pageCount++;
		}
		// this.currentPage = 1;
	}

	/**
	 * 取得总页数
	 */
	public int getPageCount() {
		return this.pageCount;
	}

	/**
	 * 是否有前一页
	 */
	public boolean isHasPreviousPage() {
		if (this.currentPage > 1) {
			return true;
		}
		return false;
	}

	/**
	 * 是否有下一页
	 */
	public boolean isHasNextPage() {
		if (this.currentPage < this.pageCount) {
			return true;
		}
		return false;
	}

	/**
	 * 转到指定页
	 */
	public void gotoPage(String page) {
		if (page == null) {
			currentPage = 1;
		} else if (page.equals(JUMP_PAGE_STEP_FIRST)) {
			currentPage = 1;
		} else if (page.equals(JUMP_PAGE_STEP_PREV)) {
			currentPage--;
		} else if (page.equals(JUMP_PAGE_STEP_NEXT)) {
			currentPage++;
		} else if (page.equals(JUMP_PAGE_STEP_LAST)) {
			currentPage = pageCount;
		} else {
			currentPage = 1;
		}
	}

	/**
	 * 转到指定页
	 */
	public void gotoPage(int page) {
		if (page < 0) {
			currentPage = 1;
		}
		if (page > pageCount) {
			currentPage = pageCount;
		}
		currentPage = page;
	}

}
